package cn.iocoder.yudao.module.wms.service.stock;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.wms.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.wms.service.warehouse.WarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.stock.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.stock.StockDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.stock.StockMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 库存汇总 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StockServiceImpl implements StockService {

    @Resource
    private StockMapper stockMapper;
    @Resource
    private WarehouseService warehouseService;

    @Override
    public Long createStock(StockSaveReqVO createReqVO) {
        // 插入
        StockDO stock = BeanUtils.toBean(createReqVO, StockDO.class);
        stockMapper.insert(stock);
        // 返回
        return stock.getId();
    }

    @Override
    public void updateStock(StockSaveReqVO updateReqVO) {
        // 校验存在
        validateStockExists(updateReqVO.getId());
        // 更新
        StockDO updateObj = BeanUtils.toBean(updateReqVO, StockDO.class);
        stockMapper.updateById(updateObj);
    }

    @Override
    public void handleStock(Long productId, WarehouseDO warehouse, BigDecimal qty) {
        // 获取当前产品库存信息
        StockDO stock = this.validateStockByProductExists(productId);
        // 判断库存是否充足
        if (qty.compareTo(BigDecimal.ZERO) < 0 && qty.abs().compareTo(stock.getStockQty()) > 0) {
            throw exception(STOCK_SHORT_QTY);
        }
        stockMapper.handleStockQty(stock.getId(), qty);
    }

    @Override
    public void deleteStock(Long id) {
        // 校验存在
        validateStockExists(id);
        // 删除
        stockMapper.deleteById(id);
    }

    @Override
    public void deleteStockByProduct(Long productId) {
        StockDO stock = validateStockByProductExists(productId);
        if (stock.getStockQty().compareTo(BigDecimal.ZERO) != 0) {
            throw exception(STOCK_NOT_EMPTY, stock.getStockQty());
        }
        stockMapper.deleteById(stock);
    }

    private StockDO validateStockExists(Long id) {
        StockDO stock = stockMapper.selectById(id);
        if (ObjectUtil.isEmpty(stock)) {
            throw exception(STOCK_NOT_EXISTS);
        }
        return stock;
    }

    private StockDO validateStockByProductExists(Long productId) {
        StockDO stock = stockMapper.selectByProduct(productId);
        if (ObjectUtil.isEmpty(stock)) {
            throw exception(STOCK_NOT_EXISTS);
        }
        return stock;
    }

    @Override
    public StockDO getStock(Long id) {
        return stockMapper.selectById(id);
    }

    @Override
    public PageResult<StockDO> getStockPage(StockPageReqVO pageReqVO) {
        return stockMapper.selectPage(pageReqVO);
    }

}